package com.welike.shibo.service.impl;

import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.enums.ResultEnum;
import com.welike.shibo.exception.BaseException;
import com.welike.shibo.service.RedisService;
import com.welike.shibo.service.SmsService;
import com.welike.shibo.service.UserService;
import com.welike.shibo.utils.Constants;
import com.welike.shibo.utils.ResultUtils;
import com.welike.shibo.utils.SmsUtils;
import com.welike.shibo.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-19 22:50
 */

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private UserService userService;

    @Resource
    private RedisService redisService;

    @Value("${redis.key.reset}")
    private String REDIS_KEY_RESET;

//    @Autowired
//    private RedisUtil redisUtil;  // 验证码存Redis

    /**
     * 阿里发送验证码to登录
     * @param phone 手机号
     * @param code 验证码
     * @return
     * @throws Exception
     */
    @Override
    public void sendMsgForLoginToAli(String phone, int code) throws Exception {
        // 手机号非空校验
        if (StringUtils.isEmpty(phone)) {
            throw new BaseException(ResultEnum.PHONE_DONT_NOT_NULL);
        }
        // 手机号正则校验
        if (!Tools.isMobile(phone)) {
            throw new BaseException(ResultEnum.PHONE_ERROR);
        }
        // 调用发送短信工具
        smsUtils.sendSmsForLogin(phone, code);
        // 验证码无需存储到Redis，直接存储到session中
        // redisUtil.setValue(phone,loginCode);
        // 设置过期时间存储
        // redisUtil.setValueWithExpire(phone,loginCode, SmsConstants.LIMIT_TIME_LOGIN);
    }

    /**
     * 腾讯云发送验证码to注册
     * @param phone 手机号
     * @param code 验证码
     * @return
     * @throws Exception
     */
    @Override
    public void sendMsgForRegisterToTencent(String phone, int code) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>(1);

        // 手机号非空校验
        if (StringUtils.isEmpty(phone)) {
            throw new BaseException(ResultEnum.PHONE_DONT_NOT_NULL);
        }
        // 手机号正则校验
        if (!Tools.isMobile(phone)) {
            throw new BaseException(ResultEnum.PHONE_ERROR);
        }
        String minutes = "30";
        // 调用腾讯云短信发送工具
        smsUtils.txSendSms(phone, code, minutes, Constants.TX_REGISTER_TEMPLATE_ID);
    }

    /**
     * 腾讯云发送验证码to修改密码
     * @param phone 手机号
     * @param code 验证码
     * @return
     * @throws Exception
     */
    @Override
    public void sendMsgForChangePwdToTencent(String phone, int code) throws Exception {
        // 手机号非空校验
        if (StringUtils.isEmpty(phone)) {
            throw new BaseException(ResultEnum.PHONE_DONT_NOT_NULL);
        }
        // 手机号正则校验
        if (!Tools.isMobile(phone)) {
            throw new BaseException(ResultEnum.PHONE_ERROR);
        }
        if (!userService.queryPhoneByPhone(phone)){
            throw new BaseException(ResultEnum.PHONE_ERROR);
        }
        String minutes = "30";
        // 调用腾讯云短信发送工具
        smsUtils.txSendSms(phone, code, minutes, Constants.TX_CHANGE_PWD_TEMPLATE_ID);
    }

    @Override
    public JsonResult resetPwdCheckSms(String phone, String code) {
        // 手机号正则校验
        if (!Tools.isMobile(phone)) {
            throw new BaseException(ResultEnum.PHONE_ERROR);
        }
        // 手机号非空校验
        if (StringUtils.isEmpty(phone)) {
            return ResultUtils.error(ResultEnum.PHONE_DONT_NOT_NULL);
        }
        if (!userService.queryPhoneByPhone(phone)){
            return ResultUtils.error(ResultEnum.PHONE_ERROR);
        }
        String checkCode = redisService.get(REDIS_KEY_RESET + phone);
        if (checkCode == null) {
            return ResultUtils.error(ResultEnum.CAPTCHA_ERROR.getCode(), "请先获取验证码");
        }
        if (!checkCode.equals(code)) {
            return ResultUtils.error(ResultEnum.CAPTCHA_ERROR.getCode(), ResultEnum.CAPTCHA_ERROR.getMsg());
        }
        return ResultUtils.success("校验成功!");
    }
}
